package com.ehome.mobile.utils;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

/**
 * Classe que faz a conexão com o web service (SOAP)
 * 
 * @author Silas M. Ferreira
 *
 */
public class KSOAP {

	private SoapObject soap;
	private SoapSerializationEnvelope envelope;
	private HttpTransportSE httpTransport;
	private Object objectReturned;
	private String url = "";
	private boolean error = false;
	private boolean encoding = false;
	private String errorMessage = "";
	private String soapAction = "";
	
	public KSOAP(String nameSpace, String method) {
		//Cria o objeto SOAP com o namespace e m�todo escolhido
		this.soap = new SoapObject(nameSpace, method);
		this.soapAction = nameSpace + "/" + method;
	}
	
	public void addProperty(String attribute, String value) {
		//Adiciona atributos ao objeto SOAP
		if (this.soap != null) {
			this.soap.addProperty(attribute, value);
		} else {
			this.error = true;
		}
	}
	
	public void addProperty(String attribute, Integer value) {
		//Adiciona atributos ao objeto SOAP
		if (this.soap != null) {
			this.soap.addProperty(attribute, value);
		} else {
			this.error = true;
		}
	}
	
	public boolean send() {
		boolean sended = false;
		/*if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}*/		
		if (this.soap != null) {
			if (!this.url.equals("")) {
				//Cria o envelope
				this.envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				
				if (this.encoding) {
					this.envelope.encodingStyle = SoapSerializationEnvelope.ENC2001;
				}
				
				//Insere o objeto SOAP no envelope
				this.envelope.setOutputSoapObject(this.soap);
				
				//Define a URL
				this.httpTransport = new HttpTransportSE(this.url);
				
				try {
					//Faz a chamada do m�todo
					this.httpTransport.call(this.soapAction, this.envelope);
					
					//Recebe o retorno do web service 
		            this.objectReturned = this.envelope.getResponse();	
		            
		            if (this.objectReturned != null) {
		            	sended = true;
		            }
				} catch (Exception e) {
					this.error = true;
					this.errorMessage = Constants.MSG_ERRO_CHAMADA;
				}
			} else {
				this.error = true;
				this.errorMessage = Constants.MSG_ERRO_URL;
			}
		} else {
			this.error = true;
			this.errorMessage = Constants.MSG_ERRO_SOAP;
		}		
		return sended;		
	}
	
	public boolean getError() {
		return this.error;
	}
	
	public String getErrorMessage() {
		return this.errorMessage;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public Object getReturn() {
		return this.objectReturned;
	}
	
	public String getReturnString() {
		return this.objectReturned.toString();
	}
}